package com.peanut.wapp.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.peanut.wapp.controller.req.BaseDataTablesReq;
import com.peanut.wapp.controller.req.HandleUserStateParam;
import com.peanut.wapp.controller.req.SubmitInfoParam;
import com.peanut.wapp.controller.res.BaseResult;
import com.peanut.wapp.controller.res.DataTablesResult;
import com.peanut.wapp.controller.res.PageResult;
import com.peanut.wapp.controller.resBean.RUserInfoBean;
import com.peanut.wapp.service.UserService;
import com.peanut.wapp.service.exception.ServiceException;
import com.peanut.wapp.util.NumUtil;

@Controller
@RequestMapping(value ="/user", produces = "application/json")
public class UserController {
	Logger logger = Logger.getLogger(UserController.class);
	
	@Autowired
	UserService userServiceImpl;
	
	@RequestMapping("/v1/submitInfo")
	@ResponseBody
	public BaseResult submitInfo(@Validated SubmitInfoParam param, BindingResult error, HttpServletRequest request, HttpServletResponse response) {
		BaseResult res = new BaseResult();
		if (error.hasErrors()) {
			res.setRescode(BaseResult.param.getCode());
			res.setMsg(error.getFieldError().getDefaultMessage());
			return res;
		}
		String phone = param.getPhoneNum();
		String name = param.getUserName();
		String host = param.getHost();
		try {
			boolean flag = userServiceImpl.submitInfo(name, phone, host);
			res.setFlag(flag);
		} catch (ServiceException e) {
			res.setServiceException(e);
		}
		return res;
	}
	
	
	/**
	 * 获取用户列表
	 **/
	@RequestMapping("/v1/list")
	@ResponseBody
	public DataTablesResult<RUserInfoBean> list(BaseDataTablesReq param, HttpServletRequest request, HttpServletResponse response) {
		DataTablesResult<RUserInfoBean> result = new DataTablesResult<RUserInfoBean>();
		try {
			int start = NumUtil.toInt(param.getStart(), 0);
			int pageSize = NumUtil.toInt(param.getLength(), 10);
			PageResult<RUserInfoBean> resData = userServiceImpl.getList(start, pageSize);
			if (resData != null) {
		        result.setData(resData.getData());
		        result.setDraw(param.getDraw()); 
		        result.setRecordsTotal(resData.getCount());
		        result.setRecordsFiltered(resData.getCount());
			}
		} catch (ServiceException e) {
			result.setServiceException(e);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result.setServiceException(new ServiceException("error", "获取异常"));
		}
		return result;
	}

	/**
	 * 处理用户信息
	 **/
	@RequestMapping("/v1/handle")
	@ResponseBody
	public BaseResult handle(@Validated HandleUserStateParam param, BindingResult error, HttpServletRequest request, HttpServletResponse response) {
		BaseResult res = new BaseResult();
		if (error.hasErrors()) {
			res.setRescode(BaseResult.param.getCode());
			res.setMsg(error.getFieldError().getDefaultMessage());
			return res;
		}
		BaseResult result = new BaseResult();
		String uid = param.getUid();
		try {
			boolean flag = userServiceImpl.handle(uid);
			result.setFlag(flag);
		} catch (ServiceException e) {
			result.setServiceException(e);
		}
		return result;
	}
}
